{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/kaggle/input/voicegender/voice.csv\n"
     ]
    }
   ],
   "source": [
    "# This Python 3 environment comes with many helpful analytics libraries installed\n",
    "# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
    "# For example, here's several helpful packages to load\n",
    "\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "# Input data files are available in the read-only \"../input/\" directory\n",
    "# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
    "\n",
    "import os\n",
    "for dirname, _, filenames in os.walk('/kaggle/input'):\n",
    "    for filename in filenames:\n",
    "        print(os.path.join(dirname, filename))\n",
    "\n",
    "# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n",
    "# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split,GridSearchCV\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import classification_report, confusion_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>meanfreq</th>\n",
       "      <th>sd</th>\n",
       "      <th>median</th>\n",
       "      <th>Q25</th>\n",
       "      <th>Q75</th>\n",
       "      <th>IQR</th>\n",
       "      <th>skew</th>\n",
       "      <th>kurt</th>\n",
       "      <th>sp.ent</th>\n",
       "      <th>sfm</th>\n",
       "      <th>...</th>\n",
       "      <th>centroid</th>\n",
       "      <th>meanfun</th>\n",
       "      <th>minfun</th>\n",
       "      <th>maxfun</th>\n",
       "      <th>meandom</th>\n",
       "      <th>mindom</th>\n",
       "      <th>maxdom</th>\n",
       "      <th>dfrange</th>\n",
       "      <th>modindx</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.059781</td>\n",
       "      <td>0.064241</td>\n",
       "      <td>0.032027</td>\n",
       "      <td>0.015071</td>\n",
       "      <td>0.090193</td>\n",
       "      <td>0.075122</td>\n",
       "      <td>12.863462</td>\n",
       "      <td>274.402906</td>\n",
       "      <td>0.893369</td>\n",
       "      <td>0.491918</td>\n",
       "      <td>...</td>\n",
       "      <td>0.059781</td>\n",
       "      <td>0.084279</td>\n",
       "      <td>0.015702</td>\n",
       "      <td>0.275862</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.066009</td>\n",
       "      <td>0.067310</td>\n",
       "      <td>0.040229</td>\n",
       "      <td>0.019414</td>\n",
       "      <td>0.092666</td>\n",
       "      <td>0.073252</td>\n",
       "      <td>22.423285</td>\n",
       "      <td>634.613855</td>\n",
       "      <td>0.892193</td>\n",
       "      <td>0.513724</td>\n",
       "      <td>...</td>\n",
       "      <td>0.066009</td>\n",
       "      <td>0.107937</td>\n",
       "      <td>0.015826</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.009014</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.054688</td>\n",
       "      <td>0.046875</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.077316</td>\n",
       "      <td>0.083829</td>\n",
       "      <td>0.036718</td>\n",
       "      <td>0.008701</td>\n",
       "      <td>0.131908</td>\n",
       "      <td>0.123207</td>\n",
       "      <td>30.757155</td>\n",
       "      <td>1024.927705</td>\n",
       "      <td>0.846389</td>\n",
       "      <td>0.478905</td>\n",
       "      <td>...</td>\n",
       "      <td>0.077316</td>\n",
       "      <td>0.098706</td>\n",
       "      <td>0.015656</td>\n",
       "      <td>0.271186</td>\n",
       "      <td>0.007990</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.015625</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.046512</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.151228</td>\n",
       "      <td>0.072111</td>\n",
       "      <td>0.158011</td>\n",
       "      <td>0.096582</td>\n",
       "      <td>0.207955</td>\n",
       "      <td>0.111374</td>\n",
       "      <td>1.232831</td>\n",
       "      <td>4.177296</td>\n",
       "      <td>0.963322</td>\n",
       "      <td>0.727232</td>\n",
       "      <td>...</td>\n",
       "      <td>0.151228</td>\n",
       "      <td>0.088965</td>\n",
       "      <td>0.017798</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.201497</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>0.562500</td>\n",
       "      <td>0.554688</td>\n",
       "      <td>0.247119</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.135120</td>\n",
       "      <td>0.079146</td>\n",
       "      <td>0.124656</td>\n",
       "      <td>0.078720</td>\n",
       "      <td>0.206045</td>\n",
       "      <td>0.127325</td>\n",
       "      <td>1.101174</td>\n",
       "      <td>4.333713</td>\n",
       "      <td>0.971955</td>\n",
       "      <td>0.783568</td>\n",
       "      <td>...</td>\n",
       "      <td>0.135120</td>\n",
       "      <td>0.106398</td>\n",
       "      <td>0.016931</td>\n",
       "      <td>0.266667</td>\n",
       "      <td>0.712812</td>\n",
       "      <td>0.007812</td>\n",
       "      <td>5.484375</td>\n",
       "      <td>5.476562</td>\n",
       "      <td>0.208274</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   meanfreq        sd    median       Q25       Q75       IQR       skew  \\\n",
       "0  0.059781  0.064241  0.032027  0.015071  0.090193  0.075122  12.863462   \n",
       "1  0.066009  0.067310  0.040229  0.019414  0.092666  0.073252  22.423285   \n",
       "2  0.077316  0.083829  0.036718  0.008701  0.131908  0.123207  30.757155   \n",
       "3  0.151228  0.072111  0.158011  0.096582  0.207955  0.111374   1.232831   \n",
       "4  0.135120  0.079146  0.124656  0.078720  0.206045  0.127325   1.101174   \n",
       "\n",
       "          kurt    sp.ent       sfm  ...  centroid   meanfun    minfun  \\\n",
       "0   274.402906  0.893369  0.491918  ...  0.059781  0.084279  0.015702   \n",
       "1   634.613855  0.892193  0.513724  ...  0.066009  0.107937  0.015826   \n",
       "2  1024.927705  0.846389  0.478905  ...  0.077316  0.098706  0.015656   \n",
       "3     4.177296  0.963322  0.727232  ...  0.151228  0.088965  0.017798   \n",
       "4     4.333713  0.971955  0.783568  ...  0.135120  0.106398  0.016931   \n",
       "\n",
       "     maxfun   meandom    mindom    maxdom   dfrange   modindx  label  \n",
       "0  0.275862  0.007812  0.007812  0.007812  0.000000  0.000000   male  \n",
       "1  0.250000  0.009014  0.007812  0.054688  0.046875  0.052632   male  \n",
       "2  0.271186  0.007990  0.007812  0.015625  0.007812  0.046512   male  \n",
       "3  0.250000  0.201497  0.007812  0.562500  0.554688  0.247119   male  \n",
       "4  0.266667  0.712812  0.007812  5.484375  5.476562  0.208274   male  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('/kaggle/input/voicegender/voice.csv')\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3168 entries, 0 to 3167\n",
      "Data columns (total 21 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   meanfreq  3168 non-null   float64\n",
      " 1   sd        3168 non-null   float64\n",
      " 2   median    3168 non-null   float64\n",
      " 3   Q25       3168 non-null   float64\n",
      " 4   Q75       3168 non-null   float64\n",
      " 5   IQR       3168 non-null   float64\n",
      " 6   skew      3168 non-null   float64\n",
      " 7   kurt      3168 non-null   float64\n",
      " 8   sp.ent    3168 non-null   float64\n",
      " 9   sfm       3168 non-null   float64\n",
      " 10  mode      3168 non-null   float64\n",
      " 11  centroid  3168 non-null   float64\n",
      " 12  meanfun   3168 non-null   float64\n",
      " 13  minfun    3168 non-null   float64\n",
      " 14  maxfun    3168 non-null   float64\n",
      " 15  meandom   3168 non-null   float64\n",
      " 16  mindom    3168 non-null   float64\n",
      " 17  maxdom    3168 non-null   float64\n",
      " 18  dfrange   3168 non-null   float64\n",
      " 19  modindx   3168 non-null   float64\n",
      " 20  label     3168 non-null   object \n",
      "dtypes: float64(20), object(1)\n",
      "memory usage: 519.9+ KB\n"
     ]
    }
   ],
   "source": [
    "#看数据缺失情况，无缺失\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>unique</th>\n",
       "      <th>top</th>\n",
       "      <th>freq</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>meanfreq</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.180907</td>\n",
       "      <td>0.0299178</td>\n",
       "      <td>0.0393633</td>\n",
       "      <td>0.163662</td>\n",
       "      <td>0.184838</td>\n",
       "      <td>0.199146</td>\n",
       "      <td>0.251124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sd</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.057126</td>\n",
       "      <td>0.0166522</td>\n",
       "      <td>0.0183632</td>\n",
       "      <td>0.0419535</td>\n",
       "      <td>0.0591551</td>\n",
       "      <td>0.0670204</td>\n",
       "      <td>0.115273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>median</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.185621</td>\n",
       "      <td>0.0363601</td>\n",
       "      <td>0.0109746</td>\n",
       "      <td>0.169593</td>\n",
       "      <td>0.190032</td>\n",
       "      <td>0.210618</td>\n",
       "      <td>0.261224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q25</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.140456</td>\n",
       "      <td>0.0486797</td>\n",
       "      <td>0.000228758</td>\n",
       "      <td>0.111087</td>\n",
       "      <td>0.140286</td>\n",
       "      <td>0.175939</td>\n",
       "      <td>0.247347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q75</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.224765</td>\n",
       "      <td>0.0236393</td>\n",
       "      <td>0.0429463</td>\n",
       "      <td>0.208747</td>\n",
       "      <td>0.225684</td>\n",
       "      <td>0.24366</td>\n",
       "      <td>0.273469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IQR</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0843094</td>\n",
       "      <td>0.0427831</td>\n",
       "      <td>0.0145577</td>\n",
       "      <td>0.0425597</td>\n",
       "      <td>0.09428</td>\n",
       "      <td>0.114175</td>\n",
       "      <td>0.252225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>skew</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.14017</td>\n",
       "      <td>4.24053</td>\n",
       "      <td>0.141735</td>\n",
       "      <td>1.64957</td>\n",
       "      <td>2.1971</td>\n",
       "      <td>2.93169</td>\n",
       "      <td>34.7255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>kurt</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>36.5685</td>\n",
       "      <td>134.929</td>\n",
       "      <td>2.06846</td>\n",
       "      <td>5.66955</td>\n",
       "      <td>8.31846</td>\n",
       "      <td>13.6489</td>\n",
       "      <td>1309.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sp.ent</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.895127</td>\n",
       "      <td>0.0449795</td>\n",
       "      <td>0.738651</td>\n",
       "      <td>0.861811</td>\n",
       "      <td>0.901767</td>\n",
       "      <td>0.928713</td>\n",
       "      <td>0.981997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sfm</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.408216</td>\n",
       "      <td>0.177521</td>\n",
       "      <td>0.0368765</td>\n",
       "      <td>0.258041</td>\n",
       "      <td>0.396335</td>\n",
       "      <td>0.533676</td>\n",
       "      <td>0.842936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.165282</td>\n",
       "      <td>0.077203</td>\n",
       "      <td>0</td>\n",
       "      <td>0.118016</td>\n",
       "      <td>0.186599</td>\n",
       "      <td>0.221104</td>\n",
       "      <td>0.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>centroid</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.180907</td>\n",
       "      <td>0.0299178</td>\n",
       "      <td>0.0393633</td>\n",
       "      <td>0.163662</td>\n",
       "      <td>0.184838</td>\n",
       "      <td>0.199146</td>\n",
       "      <td>0.251124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>meanfun</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.142807</td>\n",
       "      <td>0.0323044</td>\n",
       "      <td>0.0555653</td>\n",
       "      <td>0.116998</td>\n",
       "      <td>0.140519</td>\n",
       "      <td>0.169581</td>\n",
       "      <td>0.237636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>minfun</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0368018</td>\n",
       "      <td>0.01922</td>\n",
       "      <td>0.00977517</td>\n",
       "      <td>0.0182232</td>\n",
       "      <td>0.0461095</td>\n",
       "      <td>0.0479042</td>\n",
       "      <td>0.204082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>maxfun</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.258842</td>\n",
       "      <td>0.0300773</td>\n",
       "      <td>0.103093</td>\n",
       "      <td>0.253968</td>\n",
       "      <td>0.271186</td>\n",
       "      <td>0.277457</td>\n",
       "      <td>0.279114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>meandom</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.829211</td>\n",
       "      <td>0.525205</td>\n",
       "      <td>0.0078125</td>\n",
       "      <td>0.419828</td>\n",
       "      <td>0.765795</td>\n",
       "      <td>1.17717</td>\n",
       "      <td>2.95768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mindom</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.052647</td>\n",
       "      <td>0.0632995</td>\n",
       "      <td>0.00488281</td>\n",
       "      <td>0.0078125</td>\n",
       "      <td>0.0234375</td>\n",
       "      <td>0.0703125</td>\n",
       "      <td>0.458984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>maxdom</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.04728</td>\n",
       "      <td>3.52116</td>\n",
       "      <td>0.0078125</td>\n",
       "      <td>2.07031</td>\n",
       "      <td>4.99219</td>\n",
       "      <td>7.00781</td>\n",
       "      <td>21.8672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dfrange</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.99463</td>\n",
       "      <td>3.52004</td>\n",
       "      <td>0</td>\n",
       "      <td>2.04492</td>\n",
       "      <td>4.94531</td>\n",
       "      <td>6.99219</td>\n",
       "      <td>21.8438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>modindx</th>\n",
       "      <td>3168</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.173752</td>\n",
       "      <td>0.119454</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0997658</td>\n",
       "      <td>0.139357</td>\n",
       "      <td>0.209183</td>\n",
       "      <td>0.932374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>label</th>\n",
       "      <td>3168</td>\n",
       "      <td>2</td>\n",
       "      <td>female</td>\n",
       "      <td>1584</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         count unique     top  freq       mean        std          min  \\\n",
       "meanfreq  3168    NaN     NaN   NaN   0.180907  0.0299178    0.0393633   \n",
       "sd        3168    NaN     NaN   NaN   0.057126  0.0166522    0.0183632   \n",
       "median    3168    NaN     NaN   NaN   0.185621  0.0363601    0.0109746   \n",
       "Q25       3168    NaN     NaN   NaN   0.140456  0.0486797  0.000228758   \n",
       "Q75       3168    NaN     NaN   NaN   0.224765  0.0236393    0.0429463   \n",
       "IQR       3168    NaN     NaN   NaN  0.0843094  0.0427831    0.0145577   \n",
       "skew      3168    NaN     NaN   NaN    3.14017    4.24053     0.141735   \n",
       "kurt      3168    NaN     NaN   NaN    36.5685    134.929      2.06846   \n",
       "sp.ent    3168    NaN     NaN   NaN   0.895127  0.0449795     0.738651   \n",
       "sfm       3168    NaN     NaN   NaN   0.408216   0.177521    0.0368765   \n",
       "mode      3168    NaN     NaN   NaN   0.165282   0.077203            0   \n",
       "centroid  3168    NaN     NaN   NaN   0.180907  0.0299178    0.0393633   \n",
       "meanfun   3168    NaN     NaN   NaN   0.142807  0.0323044    0.0555653   \n",
       "minfun    3168    NaN     NaN   NaN  0.0368018    0.01922   0.00977517   \n",
       "maxfun    3168    NaN     NaN   NaN   0.258842  0.0300773     0.103093   \n",
       "meandom   3168    NaN     NaN   NaN   0.829211   0.525205    0.0078125   \n",
       "mindom    3168    NaN     NaN   NaN   0.052647  0.0632995   0.00488281   \n",
       "maxdom    3168    NaN     NaN   NaN    5.04728    3.52116    0.0078125   \n",
       "dfrange   3168    NaN     NaN   NaN    4.99463    3.52004            0   \n",
       "modindx   3168    NaN     NaN   NaN   0.173752   0.119454            0   \n",
       "label     3168      2  female  1584        NaN        NaN          NaN   \n",
       "\n",
       "                25%        50%        75%       max  \n",
       "meanfreq   0.163662   0.184838   0.199146  0.251124  \n",
       "sd        0.0419535  0.0591551  0.0670204  0.115273  \n",
       "median     0.169593   0.190032   0.210618  0.261224  \n",
       "Q25        0.111087   0.140286   0.175939  0.247347  \n",
       "Q75        0.208747   0.225684    0.24366  0.273469  \n",
       "IQR       0.0425597    0.09428   0.114175  0.252225  \n",
       "skew        1.64957     2.1971    2.93169   34.7255  \n",
       "kurt        5.66955    8.31846    13.6489   1309.61  \n",
       "sp.ent     0.861811   0.901767   0.928713  0.981997  \n",
       "sfm        0.258041   0.396335   0.533676  0.842936  \n",
       "mode       0.118016   0.186599   0.221104      0.28  \n",
       "centroid   0.163662   0.184838   0.199146  0.251124  \n",
       "meanfun    0.116998   0.140519   0.169581  0.237636  \n",
       "minfun    0.0182232  0.0461095  0.0479042  0.204082  \n",
       "maxfun     0.253968   0.271186   0.277457  0.279114  \n",
       "meandom    0.419828   0.765795    1.17717   2.95768  \n",
       "mindom    0.0078125  0.0234375  0.0703125  0.458984  \n",
       "maxdom      2.07031    4.99219    7.00781   21.8672  \n",
       "dfrange     2.04492    4.94531    6.99219   21.8438  \n",
       "modindx   0.0997658   0.139357   0.209183  0.932374  \n",
       "label           NaN        NaN        NaN       NaN  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看统计特征\n",
    "data.describe(include='all').T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "female    1584\n",
       "male      1584\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看lable的分布情况,样本正负样本分布很均衡\n",
    "data.label.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#抽出label\n",
    "y = data['label']\n",
    "\n",
    "#删除label，得出特征\n",
    "X = data.drop(columns=['label'])\n",
    "\n",
    "#划分训练集和测试集，0.25比例为测试集\n",
    "X_train,X_test, y_train,y_test= train_test_split(X,y,test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC()"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#搭建模型\n",
    "model= SVC()\n",
    "model.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#预测\n",
    "y_pred = model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "      female       0.75      0.55      0.63       404\n",
      "        male       0.63      0.81      0.71       388\n",
      "\n",
      "    accuracy                           0.68       792\n",
      "   macro avg       0.69      0.68      0.67       792\n",
      "weighted avg       0.69      0.68      0.67       792\n",
      "\n",
      "[[221 183]\n",
      " [ 73 315]]\n"
     ]
    }
   ],
   "source": [
    "#打印分类报告\n",
    "print(classification_report(y_test,y_pred))\n",
    "\n",
    "#打印混淆矩阵\n",
    "print(confusion_matrix(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#C：错误项的惩罚系数。C越大，即对分错样本的惩罚程度越大，因此在训练样本中准确率越高，但是泛化能力降低，也就是对测试数据的分类准确率降低。\n",
    "#相反，减小C的话，容许训练样本中有一些误分类错误样本，泛化能力强\n",
    "#gamma：核函数系数，对rbf核有效\n",
    "#kernel:linear线性核函数，rbf高斯核函数\n",
    "param_grid = {'C':[1,10],'gamma':[1,0.1], 'kernel':['linear','rbf']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = GridSearchCV(SVC(),param_grid,refit = True, verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 8 candidates, totalling 40 fits\n",
      "[CV] C=1, gamma=1, kernel=linear .....................................\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[CV] ...................... C=1, gamma=1, kernel=linear, total=   3.2s\n",
      "[CV] C=1, gamma=1, kernel=linear .....................................\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    3.2s remaining:    0.0s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[CV] ...................... C=1, gamma=1, kernel=linear, total=   4.8s\n",
      "[CV] C=1, gamma=1, kernel=linear .....................................\n",
      "[CV] ...................... C=1, gamma=1, kernel=linear, total=   3.5s\n",
      "[CV] C=1, gamma=1, kernel=linear .....................................\n",
      "[CV] ...................... C=1, gamma=1, kernel=linear, total=   2.1s\n",
      "[CV] C=1, gamma=1, kernel=linear .....................................\n",
      "[CV] ...................... C=1, gamma=1, kernel=linear, total=   3.4s\n",
      "[CV] C=1, gamma=1, kernel=rbf ........................................\n",
      "[CV] ......................... C=1, gamma=1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=1, kernel=rbf ........................................\n",
      "[CV] ......................... C=1, gamma=1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=1, kernel=rbf ........................................\n",
      "[CV] ......................... C=1, gamma=1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=1, kernel=rbf ........................................\n",
      "[CV] ......................... C=1, gamma=1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=1, kernel=rbf ........................................\n",
      "[CV] ......................... C=1, gamma=1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=0.1, kernel=linear ...................................\n",
      "[CV] .................... C=1, gamma=0.1, kernel=linear, total=   3.2s\n",
      "[CV] C=1, gamma=0.1, kernel=linear ...................................\n",
      "[CV] .................... C=1, gamma=0.1, kernel=linear, total=   4.9s\n",
      "[CV] C=1, gamma=0.1, kernel=linear ...................................\n",
      "[CV] .................... C=1, gamma=0.1, kernel=linear, total=   3.5s\n",
      "[CV] C=1, gamma=0.1, kernel=linear ...................................\n",
      "[CV] .................... C=1, gamma=0.1, kernel=linear, total=   2.0s\n",
      "[CV] C=1, gamma=0.1, kernel=linear ...................................\n",
      "[CV] .................... C=1, gamma=0.1, kernel=linear, total=   3.5s\n",
      "[CV] C=1, gamma=0.1, kernel=rbf ......................................\n",
      "[CV] ....................... C=1, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=0.1, kernel=rbf ......................................\n",
      "[CV] ....................... C=1, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=0.1, kernel=rbf ......................................\n",
      "[CV] ....................... C=1, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=0.1, kernel=rbf ......................................\n",
      "[CV] ....................... C=1, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=1, gamma=0.1, kernel=rbf ......................................\n",
      "[CV] ....................... C=1, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=10, gamma=1, kernel=linear ....................................\n",
      "[CV] ..................... C=10, gamma=1, kernel=linear, total=  33.0s\n",
      "[CV] C=10, gamma=1, kernel=linear ....................................\n",
      "[CV] ..................... C=10, gamma=1, kernel=linear, total=  25.3s\n",
      "[CV] C=10, gamma=1, kernel=linear ....................................\n",
      "[CV] ..................... C=10, gamma=1, kernel=linear, total=  46.0s\n",
      "[CV] C=10, gamma=1, kernel=linear ....................................\n",
      "[CV] ..................... C=10, gamma=1, kernel=linear, total=  10.7s\n",
      "[CV] C=10, gamma=1, kernel=linear ....................................\n",
      "[CV] ..................... C=10, gamma=1, kernel=linear, total=   9.1s\n",
      "[CV] C=10, gamma=1, kernel=rbf .......................................\n",
      "[CV] ........................ C=10, gamma=1, kernel=rbf, total=   0.3s\n",
      "[CV] C=10, gamma=1, kernel=rbf .......................................\n",
      "[CV] ........................ C=10, gamma=1, kernel=rbf, total=   0.3s\n",
      "[CV] C=10, gamma=1, kernel=rbf .......................................\n",
      "[CV] ........................ C=10, gamma=1, kernel=rbf, total=   0.3s\n",
      "[CV] C=10, gamma=1, kernel=rbf .......................................\n",
      "[CV] ........................ C=10, gamma=1, kernel=rbf, total=   0.3s\n",
      "[CV] C=10, gamma=1, kernel=rbf .......................................\n",
      "[CV] ........................ C=10, gamma=1, kernel=rbf, total=   0.3s\n",
      "[CV] C=10, gamma=0.1, kernel=linear ..................................\n",
      "[CV] ................... C=10, gamma=0.1, kernel=linear, total=  33.2s\n",
      "[CV] C=10, gamma=0.1, kernel=linear ..................................\n",
      "[CV] ................... C=10, gamma=0.1, kernel=linear, total=  25.3s\n",
      "[CV] C=10, gamma=0.1, kernel=linear ..................................\n",
      "[CV] ................... C=10, gamma=0.1, kernel=linear, total=  46.1s\n",
      "[CV] C=10, gamma=0.1, kernel=linear ..................................\n",
      "[CV] ................... C=10, gamma=0.1, kernel=linear, total=  10.6s\n",
      "[CV] C=10, gamma=0.1, kernel=linear ..................................\n",
      "[CV] ................... C=10, gamma=0.1, kernel=linear, total=   9.1s\n",
      "[CV] C=10, gamma=0.1, kernel=rbf .....................................\n",
      "[CV] ...................... C=10, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=10, gamma=0.1, kernel=rbf .....................................\n",
      "[CV] ...................... C=10, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=10, gamma=0.1, kernel=rbf .....................................\n",
      "[CV] ...................... C=10, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=10, gamma=0.1, kernel=rbf .....................................\n",
      "[CV] ...................... C=10, gamma=0.1, kernel=rbf, total=   0.2s\n",
      "[CV] C=10, gamma=0.1, kernel=rbf .....................................\n",
      "[CV] ...................... C=10, gamma=0.1, kernel=rbf, total=   0.2s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done  40 out of  40 | elapsed:  4.8min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(estimator=SVC(),\n",
       "             param_grid={'C': [1, 10], 'gamma': [1, 0.1],\n",
       "                         'kernel': ['linear', 'rbf']},\n",
       "             verbose=2)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 10, 'gamma': 1, 'kernel': 'linear'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = grid.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(classification_report(y_test,y_pred))\n",
    "print(confusion_matrix(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    precision    recall  f1-score   support\n",
    "\n",
    "      female       0.99      0.97      0.98       404\n",
    "        male       0.96      0.99      0.98       388\n",
    "\n",
    "    accuracy                           0.98       792\n",
    "   macro avg       0.98      0.98      0.98       792\n",
    "weighted avg       0.98      0.98      0.98       792\n",
    "\n",
    "[[390  14]\n",
    " [  4 384]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
